package ch.bfh.abcm.rems.display.subpanels.state;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;

import ch.bfh.abcm.rems.controller.MessageElement;

public class StatePanel extends JPanel implements Observer {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private static final String SUB_TITLE = "<html><h2>System State</h2></html>";
    private JLabel title;
    private JTextArea textArea;

    public StatePanel(int width, int height) {
        this.title = new JLabel(SUB_TITLE);
        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(width, height));

        this.setLayout(new BorderLayout());
        this.add(title, BorderLayout.NORTH);
        this.textArea = new JTextArea();

        Border border = BorderFactory.createLineBorder(Color.BLACK);

        setBorder(BorderFactory.createEmptyBorder(0, 10, 35, 10));
        textArea.setFont(new Font("Verdana", Font.BOLD, 12));

        textArea.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(10, 10, 10, 10)));

        this.add(textArea, BorderLayout.CENTER);
    }

    @Override
    public void update(Observable o, Object arg) {

        MessageElement element = (MessageElement) arg;
        if (!element.getMulticastType().equals(MessageElement.MulticastType.PERIODIC_NOTIFICATION)) {
            textArea.setText("");
            textArea.append(element.getMessage());
            ;
        }
    }

}
